home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / utils / mmgr / palloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  10.3 KB  |  474 lines

  1. /*
  2.  * palloc.c --
  3.  *    POSTGRES memory allocator code.
  4.  */
  5.  
  6. #include "tmp/c.h"
  7.  
  8. RcsId("$Header: /private/postgres/src/utils/mmgr/RCS/palloc.c,v 1.10 1991/11/12 20:20:29 mer Exp $");
  9.  
  10. #include "utils/mcxt.h"
  11. #include "utils/log.h"
  12.  
  13. #include "nodes/mnodes.h"
  14.  
  15. #include "utils/palloc.h"
  16.  
  17. /* ----------------------------------------------------------------
  18.  *    User library functions
  19.  * ----------------------------------------------------------------
  20.  */
  21.  
  22. #undef palloc
  23. #undef pfree
  24. #undef MemoryContextAlloc
  25. #undef MemoryContextFree
  26. #undef malloc
  27. #undef free
  28.  
  29. Pointer
  30. palloc(size)
  31.     Size    size;
  32. {
  33.     return (MemoryContextAlloc(CurrentMemoryContext, size));
  34. }
  35.  
  36. void
  37. pfree(pointer)
  38.     Pointer    pointer;
  39. {    
  40.     MemoryContextFree(CurrentMemoryContext, pointer);
  41. }
  42.  
  43. Size
  44. psize(pointer)
  45.     Pointer    pointer;
  46. {
  47.     return (PointerGetAllocSize(pointer));
  48. }
  49.  
  50. Pointer
  51. repalloc(pointer, size)
  52.     Pointer    pointer;
  53.     Size    size;
  54. {
  55.     return (MemoryContextRealloc(CurrentMemoryContext, pointer, size));
  56. }
  57.  
  58. /* ----------------------------------------------------------------
  59.  *    palloc_debug() and pfree_debug() support routines
  60.  * ----------------------------------------------------------------
  61.  */
  62.  
  63. String
  64. pcontext()
  65. {
  66.     return (String)
  67.     MemoryContextGetName(CurrentMemoryContext);
  68. }    
  69.  
  70. SLList PallocDebugList;
  71. SLList *PallocList = NULL;
  72. SLList *PfreeList = NULL;
  73. int PallocDiffTag = 0;
  74.  
  75. bool   PallocRecord;
  76. bool   PallocNoisy;
  77.  
  78. void
  79. set_palloc_debug(noisy, record)
  80.     bool   noisy;
  81.     bool   record;
  82. {
  83.     PallocNoisy = noisy;
  84.     PallocRecord = record;
  85.     SLNewList(&PallocDebugList, offsetof(PallocDebugData, Link));
  86. }
  87.  
  88. Pointer
  89. palloc_record(file, line, size, context)
  90.     String file;
  91.     int       line;
  92.     Size   size;
  93.     String context;
  94. {
  95.     Pointer p;
  96.     PallocDebugData *d, *d1;
  97.  
  98.     p = (Pointer) palloc(size);
  99.     
  100.     /* ----------------
  101.      *    note: we have to use malloc/free for the administrative
  102.      *        information because this has to work correctly when the
  103.      *          memory context stuff is not enabled.
  104.      * ----------------
  105.      */
  106.     d = (PallocDebugData *) malloc(sizeof(PallocDebugData));
  107.     
  108.     d->pointer = p;
  109.     d->size    = size;
  110.     d->line    = line;
  111.     d->file    = file;
  112.     d->context = context;
  113.     
  114.     SLNewNode(&(d->Link));
  115.     SLAddTail(&PallocDebugList, &(d->Link));
  116.     if (PallocDiffTag) {
  117.         d1 = (PallocDebugData *) malloc(sizeof(PallocDebugData));
  118.         d1->pointer = p;
  119.         d1->size    = size;
  120.         d1->line    = line;
  121.         d1->file    = file;
  122.         d1->context = context;
  123.     SLNewNode(&(d1->Link));
  124.     SLAddTail(PallocList, &(d1->Link));
  125.       }
  126.     return p;
  127. }
  128.  
  129. void
  130. pfree_remove(file, line, pointer)
  131.     String  file;
  132.     int        line;
  133.     Pointer pointer;
  134. {
  135.     PallocDebugData *d;
  136.     
  137.     d = (PallocDebugData *) SLGetHead(&PallocDebugList);
  138.     while (d != NULL) {
  139.     if (d->pointer == pointer) {
  140.         SLRemove(&(d->Link));
  141.         break;
  142.     }
  143.     d = (PallocDebugData *) SLGetSucc(&(d->Link));
  144.     }
  145.     
  146.     /* ----------------
  147.      *    at this point, if d is null it means we couldn't find
  148.      *  the pointer on the allocation list and we are accidentally
  149.      *  freeing something we shouldn't.  Otherwise, it means we
  150.      *  found the pointer so we can free the administrative info.
  151.      * ----------------
  152.      */
  153.     if (d != NULL) {
  154.     if (PallocDiffTag)
  155.         SLAddTail(PfreeList, &(d->Link));
  156.     else
  157.         free((char *)d); /* can't use pfree, see palloc_record -cim  */
  158.         if (PallocNoisy)
  159.         printf("!- f: %s l: %d p: 0x%x s: %d %s\n",
  160.                d->file, d->line, pointer, d->size, d->context);
  161.     
  162.     } else
  163.     elog(NOTICE, "pfree_remove l:%d f:%s p:0x%x %s",
  164.          line, file, pointer, "** not on list **");
  165.     
  166. }
  167.  
  168. void
  169. pfree_record(file, line, pointer)
  170.     String  file;
  171.     int        line;
  172.     Pointer pointer;
  173. {
  174.     /* ----------------
  175.      *  scan our allocation list for the appropriate debug entry.
  176.      *  when we find the entry in the list, remove it.  then pfree()
  177.      *  the pointer itself.
  178.      * ----------------
  179.      */
  180.     pfree_remove(file, line, pointer);
  181.     pfree(pointer);
  182. }
  183.  
  184. void
  185. dump_palloc_list(caller, verbose)
  186.     String caller;
  187.     bool   verbose;
  188. {
  189.     PallocDebugData *d;
  190.     int         i;
  191.     Size         total;
  192.  
  193.     if (! PallocRecord)
  194.     return;
  195.     
  196.     d = (PallocDebugData *) SLGetHead(&PallocDebugList);
  197.     i = 0;
  198.     total = 0;
  199.     
  200.     while (d != NULL) {
  201.     if (verbose)
  202.         printf("!! f: %s l: %d p: 0x%x s: %d %s\n",
  203.            d->file, d->line, d->pointer, d->size, d->context);
  204.     i++;
  205.     total += d->size;
  206.     
  207.     d = (PallocDebugData *) SLGetSucc(&(d->Link));
  208.     }
  209.     
  210.     printf("\n!! dump_palloc_list: %s items: %d totalsize: %d\n",
  211.        caller, i, total);
  212. }
  213.  
  214. /* ----------------------------------------------------------------
  215.  *    palloc_debug() and pfree_debug()
  216.  * ----------------------------------------------------------------
  217.  */
  218.  
  219. Pointer
  220. palloc_debug(file, line, size)
  221.     String file;
  222.     int       line;
  223.     Size   size;
  224. {
  225.     Pointer p;
  226.     String context = pcontext();
  227.     
  228.     if (PallocRecord)
  229.     p = palloc_record(file, line, size, context);
  230.     else
  231.     p = palloc(size);
  232.     
  233.     if (PallocNoisy)
  234.     printf("!+ f: %s l: %d p: 0x%x s: %d %s\n",
  235.            file, line, p, size, context);
  236.     
  237.     return p;
  238. }
  239.  
  240. void
  241. pfree_debug(file, line, pointer)
  242.     String  file;
  243.     int        line;
  244.     Pointer pointer;
  245. {
  246.     String context = pcontext();
  247.     
  248.     if (PallocRecord)
  249.     pfree_record(file, line, pointer);
  250.     else
  251.     pfree(pointer);
  252.     
  253.     if (PallocNoisy && !PallocRecord)
  254.     printf("!- f: %s l: %d p: 0x%x s: ? %s\n",
  255.            file, line, pointer, context);
  256. }
  257.  
  258. void
  259. alloc_set_message(file, line, pointer, set)
  260.     String   file;
  261.     int         line;
  262.     Pointer  pointer;
  263.     Pointer  set;
  264. {
  265.     if (PallocRecord)
  266.     pfree_remove(file, line, pointer);
  267. }
  268.  
  269. void
  270. free_palloc_list(list)
  271. SLList *list;
  272. {
  273.     PallocDebugData *d;
  274.     PallocDebugData *d1;
  275.  
  276.     d = (PallocDebugData*)SLGetHead(list);
  277.     while (d != NULL) {
  278.     d1 = d;
  279.     d = (PallocDebugData*)SLGetSucc(&(d->Link));
  280.     free((char *)d1);
  281.       }
  282.     free((char *)list);
  283. }
  284.  
  285. void
  286. start_palloc_list()
  287. {
  288.     if (PallocRecord)
  289.         free_palloc_list(&PallocDebugList);
  290.     SLNewList(&PallocDebugList, offsetof(PallocDebugData, Link));
  291.     PallocRecord = 1;
  292. }
  293.  
  294. void
  295. print_palloc_list()
  296. {
  297.     PallocDebugData *d;
  298.     int         i;
  299.     Size         total;
  300.  
  301.     if (! PallocRecord)
  302.     return;
  303.     
  304.     d = (PallocDebugData *) SLGetHead(&PallocDebugList);
  305.     i = 0;
  306.     total = 0;
  307.     
  308.     while (d != NULL) {
  309.     printf("!! f: %s l: %d p: 0x%x s: %d %s\n",
  310.            d->file, d->line, d->pointer, d->size, d->context);
  311.     i++;
  312.     total += d->size;
  313.     
  314.     d = (PallocDebugData *) SLGetSucc(&(d->Link));
  315.     }
  316.     
  317.     printf("\n!! print_palloc_list: items: %d totalsize: %d\n", i, total);
  318. }
  319.  
  320. void
  321. start_palloc_diff_list()
  322. {
  323.     if (PallocList)
  324.        free_palloc_list(PallocList);
  325.     if (PfreeList)
  326.        free_palloc_list(PfreeList);
  327.     PallocList = (SLList*)malloc(sizeof(SLList));
  328.     PfreeList = (SLList*)malloc(sizeof(SLList));
  329.     SLNewList(PallocList, offsetof(PallocDebugData, Link));
  330.     SLNewList(PfreeList, offsetof(PallocDebugData, Link));
  331.     PallocDiffTag = 1;
  332. }
  333.  
  334. void
  335. end_palloc_diff_list()
  336. {
  337.     if (PallocList)
  338.        free_palloc_list(PallocList);
  339.     if (PfreeList)
  340.        free_palloc_list(PfreeList);
  341.     PallocList = NULL;
  342.     PfreeList = NULL;
  343.     PallocDiffTag = 0;
  344. }
  345.  
  346. void
  347. print_palloc_diff_list()
  348. {
  349.     PallocDebugData *d, *d1, *tempd;
  350.     int i, total;
  351.  
  352.     d = (PallocDebugData *) SLGetHead(PallocList);
  353.     total = 0;
  354.     i = 0;
  355.     while (d != NULL) {
  356.     printf("!! f: %s l: %d p: 0x%x s: %d %s\n",
  357.            d->file, d->line, d->pointer, d->size, d->context);
  358.     i++;
  359.     total += d->size;
  360.     d = (PallocDebugData *) SLGetSucc(&(d->Link));
  361.     }
  362.     printf("\n!! new pallocs: items: %d totalsize: %d\n\n", i, total);
  363.     d = (PallocDebugData *) SLGetHead(PfreeList);
  364.     total = 0;
  365.     i = 0;
  366.     while (d != NULL) {
  367.     printf("!! f: %s l: %d p: 0x%x s: %d %s\n",
  368.            d->file, d->line, d->pointer, d->size, d->context);
  369.     i++;
  370.     total += d->size;
  371.     d = (PallocDebugData *) SLGetSucc(&(d->Link));
  372.     }
  373.     printf("\n!! new pfrees: items: %d totalsize: %d\n\n", i, total);
  374.     total = 0;
  375.     i = 0;
  376.     d = (PallocDebugData *) SLGetHead(PallocList);
  377.     while (d !=NULL) {
  378.     d1 = (PallocDebugData *) SLGetHead(PfreeList);
  379.     while (d1 != NULL) {
  380.         if (strcmp(d->file, d1->file) == 0 && d->line == d1->line &&
  381.         d->size == d1->size) {
  382.         SLRemove(&(d1->Link));
  383.         free((char *)d1);
  384.         SLRemove(&(d->Link));
  385.         tempd = d;
  386.         break;
  387.           }
  388.         d1 = (PallocDebugData *) SLGetSucc(&(d1->Link));
  389.       }
  390.        if (d1 == NULL) {
  391.        printf("!! f: %s l: %d p: 0x%x s: %d %s\n",
  392.           d->file, d->line, d->pointer, d->size, d->context);
  393.        i++;
  394.        total += d->size;
  395.      }
  396.        d = (PallocDebugData *) SLGetSucc(&(d->Link));
  397.        if (d1 != NULL)
  398.        free((char *)tempd);
  399.      }
  400.     printf("\n!! unfreed allocs: items: %d totalsize: %d\n\n", i, total);
  401. }
  402.  
  403. char *
  404. malloc_debug(file, line, size)
  405. String file;
  406. int line;
  407. int size;
  408. {
  409.     char *p;
  410.     PallocDebugData *d, *d1;
  411.  
  412.     p = (char*)malloc(size);
  413.     if (PallocRecord) {
  414.     d = (PallocDebugData*)malloc(sizeof(PallocDebugData));
  415.     d->pointer = (Pointer)p;
  416.     d->size = size;
  417.     d->line = line;
  418.     d->file = file;
  419.     d->context = "** Direct Malloc **";
  420.     SLNewNode(&(d->Link));
  421.     SLAddTail(&PallocDebugList, &(d->Link));
  422.     if (PallocDiffTag) {
  423.         d1 = (PallocDebugData*)malloc(sizeof(PallocDebugData));
  424.         d1->pointer = p;
  425.         d1->size = size;
  426.         d1->line = line;
  427.         d1->file = file;
  428.         d1->context = "** Direct Malloc **";
  429.         SLNewNode(&(d1->Link));
  430.         SLAddTail(PallocList, &(d1->Link));
  431.       }
  432.        }
  433.     if (PallocNoisy)
  434.     printf("!+ f: %s l: %d p: 0x%x s: %d ** Direct Malloc **\n",
  435.            file, line, p, size);
  436.     return p;
  437. }
  438.  
  439. free_debug(file, line, p)
  440. String file;
  441. int line;
  442. char *p;
  443. {
  444.     PallocDebugData *d;
  445.  
  446.     if (PallocRecord) {
  447.     d = (PallocDebugData*)SLGetHead(&PallocDebugList);
  448.     while (d != NULL) {
  449.         if (d->pointer == (Pointer)p) {
  450.         SLRemove(&(d->Link));
  451.         break;
  452.           }
  453.         d = (PallocDebugData*)SLGetSucc(&(d->Link));
  454.       }
  455.     if (d != NULL) {
  456.         if (PallocDiffTag)
  457.         SLAddTail(PfreeList, &(d->Link));
  458.         else
  459.         free((char *)d);
  460.       }
  461.     else
  462.         elog(NOTICE, "pfree_remove l:%d f:%s p:0x%x %s",
  463.          line, file, p, "** not on list **");
  464.       }
  465.     if (PallocNoisy)
  466.     if (d != NULL)
  467.         printf("!- f: %s l: %d p: 0x%x s: %d ** Direct Malloc **\n",
  468.                d->file, d->line, p, d->size);
  469.     else
  470.         printf("!- f: %s l: %d p: 0x%x s: ? ** Direct Malloc **\n",
  471.                file, line, p);
  472.     free(p);
  473. }
  474.